{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "5f976f46-9d51-407e-8eeb-e46cfdd22ae1",
   "metadata": {},
   "outputs": [],
   "source": [
    "import openai\n",
    "\n",
    "openai.api_key = \"sk-your-key\"\n",
    "from llama_index.agent import OpenAIAgent"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "538e1519-2cfe-40f4-a861-65139156d2fe",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Load the Plugin\n",
    "import requests\n",
    "import yaml\n",
    "\n",
    "f = requests.get(\n",
    "    \"https://raw.githubusercontent.com/sisbell/chatgpt-plugin-store/main/manifests/today-currency-converter.oiconma.repl.co.json\"\n",
    ").text\n",
    "manifest = yaml.safe_load(f)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b988f94e-6559-4404-9ba8-b41d4e4da770",
   "metadata": {},
   "outputs": [],
   "source": [
    "from llama_index.tools.chatgpt_plugin.base import ChatGPTPluginToolSpec\n",
    "from llama_index.tools.requests.base import RequestsToolSpec\n",
    "\n",
    "requests_spec = RequestsToolSpec()\n",
    "plugin_spec = ChatGPTPluginToolSpec(manifest)\n",
    "# OR\n",
    "plugin_spec = ChatGPTPluginToolSpec(\n",
    "    manifest_url=\"https://raw.githubusercontent.com/sisbell/chatgpt-plugin-store/main/manifests/today-currency-converter.oiconma.repl.co.json\"\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "475cd741-e53e-4e9c-857b-6cb52641af0d",
   "metadata": {},
   "outputs": [],
   "source": [
    "agent = OpenAIAgent.from_tools(\n",
    "    [*plugin_spec.to_tool_list(), *requests_spec.to_tool_list()], verbose=True\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "88175b8a-0345-44d2-8b78-983d7e7415bb",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "=== Calling Function ===\n",
      "Calling function: describe_plugin with args: {}\n",
      "Got output: \n",
      "            'human_description': Converts currency values based on the latest exchange rates.\n",
      "            'model_description': Converts currency values based on the latest exchange rates.\n",
      "        \n",
      "========================\n",
      "The OpenAPI plugin that was loaded is a plugin that converts currency values based on the latest exchange rates. It provides functionality to retrieve exchange rates and perform currency conversions.\n"
     ]
    }
   ],
   "source": [
    "print(agent.chat(\"Can you give me info on the OpenAPI plugin that was loaded\"))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "39e42a58-a6db-47f0-8bd7-a4c3728f0839",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "=== Calling Function ===\n",
      "Calling function: load_openapi_spec with args: {}\n",
      "Got output: [Document(id_='bd92f5ee-3c31-4938-a75f-004d6e451181', embedding=None, metadata={}, excluded_embed_metadata_keys=[], excluded_llm_metadata_keys=[], relationships={}, hash='b43045dea83f8c9bbefe5516329054c5ec962524078a7c9f6055af71666dd4bb', text=\"{'servers': [{'url': 'https://today-currency-converter.oiconma.repl.co/'}], 'description': 'Allows users to convert currency values based on the latest exchange rates.', 'endpoints': [('GET /currency-converter', None, {'parameters': [{'name': 'from', 'in': 'query', 'description': 'The currency to convert from', 'required': True, 'schema': {'type': 'string'}}, {'name': 'to', 'in': 'query', 'description': 'The currency to convert to', 'required': True, 'schema': {'type': 'string'}}, {'name': 'amount', 'in': 'query', 'description': 'The amount to convert', 'required': True, 'schema': {'type': 'string'}}], 'responses': {'description': 'OK', 'content': {'application/json': {'schema': {'type': 'object', 'properties': {'result': {'type': 'object', 'properties': {'info': {'type': 'object', 'properties': {'rate': {'type': 'number', 'format': 'float', 'description': 'The conversion rate between the two currencies'}}}, 'date': {'type': 'string', 'description': 'The date of the conversion'}, 'result': {'type': 'number', 'format': 'float', 'description': 'The converted amount'}}}}}}}}})]}\", start_char_idx=None, end_char_idx=None, text_template='{metadata_str}\\n\\n{content}', metadata_template='{key}: {value}', metadata_seperator='\\n')]\n",
      "========================\n",
      "=== Calling Function ===\n",
      "Calling function: get_request with args: {\n",
      "  \"url\": \"https://today-currency-converter.oiconma.repl.co/currency-converter?from=EUR&to=CAD&amount=100\"\n",
      "}\n",
      "Got output: {'date': '2023-07-24', 'historical': False, 'info': {'rate': 1.469716}, 'motd': {'msg': 'If you or your company use this project or like what we doing, please consider backing us so we can continue maintaining and evolving this project.', 'url': 'https://exchangerate.host/#/donate'}, 'query': {'amount': 100, 'from': 'EUR', 'to': 'CAD'}, 'result': 146.971621, 'success': True}\n",
      "========================\n",
      "The conversion of 100 euros to CAD is 146.971621 CAD.\n"
     ]
    }
   ],
   "source": [
    "print(agent.chat(\"Can you convert 100 euros to CAD\"))"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
